package t2h.activities;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordFilter;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import t2h.listsong.AccentRemover;
import t2h.valueobjects.SongVO;

import com.sun.lwuit.list.ListModel;
import com.sun.lwuit.list.DefaultListModel;

public class ListSongRecord {
	private RecordStore recordID = null;
    private RecordStore recordName = null;
    private RecordStore recordAuthor = null;
    private RecordStore recordLyric = null;
    private RecordEnumeration recordEnumeration = null;
	static ListModel r = new DefaultListModel();
    ReadXmlFileWithoutLyric readxml = new ReadXmlFileWithoutLyric();
    static ListModel listsong = new DefaultListModel();
    
    public ListSongRecord() throws IOException, SAXException, ParserConfigurationException{
    	ReadXmlRecord readxml = new ReadXmlRecord();
    	recordID = readxml.getaRecordID();
    	recordAuthor = readxml.getaRecordAuthor();
    	recordLyric = readxml.getaRecordLyric();
    	recordName = readxml.getaRecordName();
    }
    
   /* public void ConvertToRecord(){
    	
 	    try {
 		    try {
				listsong = readxml.getListKara();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ParserConfigurationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
 		    recordID = RecordStore.openRecordStore("recordID", true);
 		    recordName = RecordStore.openRecordStore("recordName", true);
 		    recordAuthor = RecordStore.openRecordStore("recordAuthor", true);
 		    recordLyric = RecordStore.openRecordStore("recordLyric", true);
 		    for (int i = 0; i < listsong.getSize(); i++) {
 		    	SongVO temp = (SongVO) listsong.getItemAt(i);
 		    	byte[] byteOutputDataID = temp.getId().getBytes();
 		    	byte[] byteOutputDataName = temp.getName().getBytes();
 		    	byte[] byteOutputDataAuthor = temp.getAuthor().getBytes();
 		    	byte[] byteOutputDataLyric = temp.getLyric().getBytes();
 		    	recordID.addRecord(byteOutputDataID, 0, byteOutputDataID.length);
 		    	recordName.addRecord(byteOutputDataName, 0, byteOutputDataName.length);
 		    	recordAuthor.addRecord(byteOutputDataAuthor, 0, byteOutputDataAuthor.length);
 		    	recordLyric.addRecord(byteOutputDataLyric, 0, byteOutputDataLyric.length);
 		    }
		} catch (RecordStoreFullException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }*/
    
    public ListModel searchIn(String searchstring) throws InvalidRecordIDException, RecordStoreException {
    	

    	Filter m = new Filter(searchstring);
    	if(r != null)
    		r = new DefaultListModel();
	    int sizeList;
		sizeList = recordName.getNumRecords();
		//for( int i = 0; i < sizeList; i++ ) { 
			//if( m.matches(recordName.getRecord(i) ) ) {
				SongVO a = new SongVO();
	        	String testID = new String(recordID.getRecord(0));
	        	String testName = new String(recordName.getRecord(0));
	        	String testAuthor = new String(recordAuthor.getRecord(0));
	        	String testLyric = new String(recordLyric.getRecord(0));
	        	a.setId(testID);
	        	a.setName(testName);
	        	a.setAuthor(testAuthor);
	        	a.setLyric(testLyric);
	            r.addItem(a);
	      // }
	    //}	      
	    return r;
	}
}

class Filter implements RecordFilter {
	  private String search = null;

	  private ByteArrayInputStream inputstream = null;

	  private DataInputStream datainputstream = null;

	  AccentRemover acc = new AccentRemover();
	  
	  public Filter(String search) {
	    this.search = acc.toRemoveVNese(search.toLowerCase());
	  }

	  public boolean matches(byte[] suspect) {
	    String string = new String(suspect).toLowerCase();
	    string = acc.toRemoveVNese(string);
	    if (string != null && string.indexOf(search) != -1)
	      return true;
	    else
	      return false;
	  }

	  public void filterClose() {
	    try {
	      if (inputstream != null) {
	        inputstream.close();
	      }
	      if (datainputstream != null) {
	        datainputstream.close();
	      }
	    } catch (Exception error) {
	    }
	  }
	}
